package MessageQueue;

import java.util.LinkedList;

public class MessageQueue {
    private LinkedList<Message> list=new LinkedList<>();
    //初始化容量
    private int capcity;
    public MessageQueue(int capcity){
        this.capcity=capcity;
    }

    //从消息队列中获取资源
    public Message take() throws InterruptedException {
        synchronized (list){
            while(list.isEmpty()){
                list.wait();
            }
            Message message=list.removeFirst();
            list.notify();

            return message;
        }
    }

    //往消息队列中放入资源
    public void put(Message message) throws InterruptedException {
        synchronized (list){
            while(list.size() == capcity){
                //已经存满了，直接等待
                list.wait();
            }
            list.addLast(message);
            list.notify();
        }

    }



}

class Message{
    private Object msg;
    private int id;

    public Object getMsg() {
        return msg;
    }

    public int getId() {
        return id;
    }
}
